Automation word compare

Hello,

I would like to convert the following vba into dxl, currently have I limited experience using OleAutoObj; any help would be greatly appreciated. Thank you.

 

Sub CompareFiles()
    Dim doc1 As String
    Dim doc2 As String
    Dim doc3 As String
    
    doc1 = InputBox("Enter full path of base document:")
    doc2 = InputBox("Enter full path of comparison document:")
    doc3 = InputBox("Enter full path for document comparisons to be saved(*.docm):")
    
    Dim objDocA As Word.Document
    Dim objDocB As Word.Document
    Dim objDocC As Word.Document
    
    ' Open the original document
    Set objDocA = Documents.Open(doc1)
    ' Open the compare against document
    Set objDocB = Documents.Open(doc2)
    
    Set objDocC = Application.MergeDocuments(objDocA, objDocB, _
        Destination:=wdCompareDestinationNew, _
        Granularity:=wdGranularityWordLevel, _
        CompareFormatting:=True, _
        CompareCaseChanges:=True, _
        CompareWhitespace:=True, _
        CompareTables:=True, _
        CompareHeaders:=True, _
        CompareFootnotes:=True, _
        CompareTextboxes:=True, _
        CompareFields:=True, _
        CompareComments:=True, _
        CompareMoves:=True, _
        OriginalAuthor:="rpe", _
        RevisedAuthor:="rpe", _
        FormatFrom:=wdMergeFormatFromPrompt)

    objDocC.SaveAs FileName:=doc3, FileFormat:=wdFormatXMLDocumentMacroEnabled
    objDocC.Close
    
    objDocA.Close
    objDocB.Close
End Sub

 


EHcnck - Thu Mar 19 21:43:24 EDT 2015

Re: Automation word compare
woodpryan - Fri Mar 20 01:09:25 EDT 2015

This should steer you at least near the right direction:

#include ""utils/ThaadOleDotInc.inc"//Modification of ole.inc, delivered with DOORS by IBM
#include "utils/fileops.dxl"//Provided by DOORS. Gives us some file functions
#include "utils/doctools/rtfattr.inc"//Provided by DOORS. Rich Text Format stuff
#include "utils/doctools/itfutil.inc"

OleAutoObj objDocs         = null;
OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoObj objTables       = null;
OleAutoObj objInlineShapes = null;
OleAutoObj objSel          = null;
OleAutoArgs objArgBlock    = null;

Skip wordStyleNames      = null; // skip list acts as a zero-based array
Skip styleOKNames        = null; // list of styles from "paragraph style" attr that we have verified
Skip wordStyleFonts      = null;  // skip list of fonts corresponding to style names
Skip wordStyleHasBullets = null; // skip list storing word styles which have bullets
Skip wordStyleHasIndents = null; // skip list storing word styles which have indents
int  noWordStyles        = 0;//The number of Word Styles
bool styleWarn           = false;


const string fontTableBeginning = (NLS_("\\sectd\\deff0{\\fonttbl"));

// fonts in this part of the font table will be used for anything
// outside the Latin-1 character set
const string fontTableEnd =
(NLS_("{\\f1\\fswiss\\fprq2\\fcharset0 Arial;}
{\\f2\\froman\\fprq2\\fcharset2 Symbol;}
{\\f3\\fswiss\\fprq2\\fcharset128 Arial Unicode MS;}
{\\f4\\fswiss\\fprq2\\fcharset129 Arial Unicode MS;}
{\\f5\\fswiss\\fprq2\\fcharset130 Arial Unicode MS;}
{\\f6\\fswiss\\fprq2\\fcharset134 Arial Unicode MS;}
{\\f7\\fswiss\\fprq2\\fcharset136 Arial Unicode MS;}
{\\f8\\fswiss\\fcharset161 Arial;}
{\\f9\\fswiss\\fcharset162 Arial;}
{\\f10\\fswiss\\fcharset163 Arial;}
{\\f11\\fswiss\\fcharset177 Arial;}
{\\f12\\fswiss\\fcharset178 Arial;}
{\\f13\\fswiss\\fcharset186 Arial;}
{\\f14\\fswiss\\fcharset204 Arial;}
{\\f15\\fswiss\\fprq2\\fcharset222 Arial Unicode MS;}
{\\f16\\fswiss\\fcharset238 Arial;}
}"));

/* Function taken directly from word.dxl provided by DOORS
   Purpose: This function checks that an oleMthod, oleGet, or olePut
            function did not return an error in the form of a string.
            Wrap this function around one of those three functions, and
            if it returns an error, this function will print it in a message box.
*/
void vbaCheckRes(string res)
{
    if(res != (NLS_("")))
    {
        ack (dxlStrformat(LS_("String_OLE_method_failed_s",NLSTEMP_("OLE method failed: %s")), res))
    }
}

/* Function taken directly from word.dxl provided by DOORS
*/
void getWordStyleNames(OleAutoObj objDoc)
{
    OleAutoObj objStyles = null;
    OleAutoObj objStyle = null;
    OleAutoObj objFont = null;
    OleAutoObj objParaFormat = null;
    OleAutoObj objListTemplate = null;
    string styleName = null;
    string fontName = null;
    int styleType = 0;
    int leftIndent = 0;

    wordStyleNames = create;
    wordStyleFonts = create;
    wordStyleHasBullets = create;
    wordStyleHasIndents = create;

    int count, stylesToCheck;
    noWordStyles = 0;

    vbaCheckRes(oleGet(objDoc, cPropertyStyles, objStyles));

    if (null objStyles)
    {
        ack LS_("String_Unable_to_get_handle_on_styles_collection",NLSTEMP_("Unable to get handle on styles collection"));
        clearWordState;
    }

    vbaCheckRes(oleGet(objStyles, cPropertyCount, stylesToCheck));

    for (count = 1; count <= stylesToCheck; count++)
    {
        clear objArgBlock;
        put(objArgBlock, count);

        vbaCheckRes(oleMethod(objStyles, cMethodItem, objArgBlock, objStyle));

        if (null objStyle)
        {
            ack LS_("String_Unable_to_get_handle_on_style",NLSTEMP_("Unable to get handle on style"));
            clearWordState();
        }

        // Don't use character styles
        vbaCheckRes(oleGet(objStyle, cPropertyType, styleType));

        if (styleType != wdStyleTypeParagraph)
        {
            closeIfNonNull objStyle;
            continue;
        }

        styleName = (NLS_(""));
        fontName = (NLS_(""));
        leftIndent = 0;

        vbaCheckRes(oleGet(objStyle, cPropertyNameLocal, styleName));

        if (null styleName)
        {
            ack LS_("String_Unable_to_get_style_name",NLSTEMP_("Unable to get style name"));
            clearWordState;
        }
        put(wordStyleNames,noWordStyles,styleName);

        // Now check if the style involves bullets or indenting

        oleGet(objStyle, cPropertyListTemplate, objListTemplate);
        if (!(null objListTemplate))
        { // The style has some kind of bulleting applied
            put(wordStyleHasBullets, styleName, styleName);
            oleCloseAutoObject objListTemplate;
        }
        oleGet(objStyle, cPropertyParagraphFormat, objParaFormat);
        oleGet(objParaFormat, cPropertyLeftIndent, leftIndent);
        if (leftIndent > 0)
        { // The style has some kind of indenting applied
            put(wordStyleHasIndents, styleName, styleName);
        }
        closeIfNonNull objParaFormat;

        // get font name so we can set the correct default font
        vbaCheckRes(oleGet(objStyle, cPropertyFont, objFont));

        if (null objFont)
        {
            ack LS_("String_Unable_to_get_handle_on_font",NLSTEMP_("Unable to get handle on font"));
            clearWordState;
        }

        vbaCheckRes(oleGet(objFont, cPropertyName, fontName));

        if (null fontName)
        {
            ack LS_("String_Unable_to_get_font_name",NLSTEMP_("Unable to get font name"));
            clearWordState();
        }

        closeIfNonNull objFont;
        closeIfNonNull objStyle;

        put(wordStyleFonts,styleName,fontName);

        noWordStyles++;
    }

    closeIfNonNull objStyles;

    // <sigh>, Word gives us styles in almost-but-not-quite alpha order,
    // we really do need to sort them
    string tempWordStyles[noWordStyles];

    for (count = 0; count < noWordStyles; count++)
    {
        find(wordStyleNames, count, tempWordStyles[count]);
    }

    sort tempWordStyles;
    delete wordStyleNames;
    wordStyleNames = create;


    for (count = 0; count < noWordStyles; count++)
    {
        put(wordStyleNames, count, tempWordStyles[count]);
    }
}


/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/* Function taken directly from word.dxl provided by DOORS
*/
bool wordPreChecksVBA()
{
    Object curro = current Object;
    Module m     = current Module;

    // Make Word visible
    makeVisible objWord;

    // Get documents collection
    checkRes(oleGet(objWord, cPropertyDocuments, objDocs));

    if (null objDocs)
    {
        ack LS_("String_Unable_to_get_Documents_collection",NLSTEMP_("Unable to get Documents collection"));
        return false;
    }

    // Make a new document
    checkRes(oleMethod(objDocs,cMethodAdd));

    // Get a handle on the document
    checkRes(oleGet(objWord,cPropertyActiveDocument,objDoc));

    if (null objDoc)
    {
        ack LS_("String_Unable_to_get_active_document",NLSTEMP_("Unable to get active document"));
        halt();
    }

    // Get a handle on the selection object
    checkRes(oleGet(objWord,cPropertySelection,objSel));

    if (null objSel)
    {
        ack LS_("String_Unable_to_get_selection_object",NLSTEMP_("Unable to get selection object"));

        halt();
    }

    // Get a handle on the shapes collection
    checkRes(oleGet(objDoc,cPropertyInlineShapes,objInlineShapes));

    if (null objInlineShapes)
    {
        ack LS_("String_Unable_to_get_inline_shapes_collection",NLSTEMP_("Unable to get inline shapes collection"));
        halt();
    }

    // Get a handle on the tables collection
    checkRes(oleGet(objDoc,cPropertyTables,objTables));

    if (null objTables)
    {
        ack LS_("String_Unable_to_get_tables_collection",NLSTEMP_("Unable to get tables collection"));
        halt();
    }

    return true;
}

/* Function taken directly from word.dxl provided by DOORS
*/
string makeFontTableFromStyle(string styleName)
{
// build a font table with the appropriate default font for the style
    string fontName = (NLS_(""))
    string defaultFontEntry
    string fontTable

    find(wordStyleFonts, styleName, fontName)

    defaultFontEntry = (NLS_("{\\f0\\fnil ")) fontName (NLS_(";}"))
    fontTable = fontTableBeginning defaultFontEntry fontTableEnd

    return fontTable
}

/*  Function:   toggleTrackRevision()
    Purpose:    toggles trackRevision on and off in the document
*/
void toggleTrackRevisions()
{
    bool track = false;

    oleGet(objDoc, cPropertyTrackRevisions, track);
    olePut(objDoc, cPropertyTrackRevisions, !track);
    isTrackRevisions = !track;
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name.
*/
void createDocumentDiff(string original, string revised)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

    oleGet(objWord, cPropertyDocuments, objDocuments);
    if(null == objDocuments)
    {
        logError("Could not retrieve the Documents collection from MS Word");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, original);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc);
    if(null == objOriginalDoc)
    {
        closeIfNonNull(objDocuments);
        logError("Could not retrieve the Original Document from the Documents Collection.");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, revised);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
    if(null == objRevisedDoc)
    {
        closeIfNonNull(objDocuments);
        closeIfNonNull(objOriginalDoc);
        logError("Could not retrieve the Revised Document from the Documents Collection.");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, objOriginalDoc);//originalDocument
    put(objArgBlock, objRevisedDoc);//revisedDocument
    put(objArgBlock, wdCompareDestinationNew);//Destination
    put(objArgBlock, wdGranularityCharLevel);//granularity
    put(objArgBlock, false);//compareFormatting
    put(objArgBlock, true);//compareCaseChanges
    put(objArgBlock, true);//compareWhitespace
    put(objArgBlock, true);//compareTables
    put(objArgBlock, false);//compareHeaders
    put(objArgBlock, false);//compareFootnotes
    put(objArgBlock, false);//compareTextboxes
    put(objArgBlock, false);//compareFields
    put(objArgBlock, false);//compareComments
    put(objArgBlock, false);//compareMoves


    oleMethod(objWord, cMethodCompareDocuments, objArgBlock);

    oleGet(objWord, cPropertyDocuments, objDocuments);

    clear(objArgBlock);
    put(objArgBlock, 1);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);
    oleMethod(objDoc, cMethodActivate);

    closeIfNonNull(objDocuments);
    closeIfNonNull(objOriginalDoc);
    closeIfNonNull(objRevisedDoc);
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/*  Function:   activateDocument(string)
    Purpose:    Takes in a document name, finds the document
                in the documents collection by name, and makes that document active.
    Parameters: The name of the document to make active.
*/
void activateDocument(string docName)
{
    OleAutoObj objDocuments = null;
    string     documentName = null;

    oleGet(objWord, cPropertyDocuments, objDocuments);
    if(null == objDocuments)
    {
        logError("Could not retrieve the Documents collection from MS Word");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);

    if(null == objDoc)
    {
        logError("Could not find a document in the collection named " docName);
        return;
    }

    oleMethod(objDoc, cMethodActivate);
    oleGet(objWord, cPropertySelection, objSel);
    activeDocumentName = docName;
}

/*  Function:   saveDocument(string)
    Purpose:    saves the active document using the given
                document name.
    Parameters: the name of the document
*/
void saveDocument(string docName)
{
    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
}

Think of an OleAutoObj like this. Each class in VBA (Word, Document, Selection, etc.) is an OleAutoObj. Each function available to a class is a cMethod. Each cMethod is define in Ole.inc as cMethodName = "Name". When you want to call a method in VBA on a class, you must pass it arguments. Those arguments go, in order, into the objArbBlock, which is a container for the arguments. When you call the method, you must store the return value in something else (another OleAutoObj, maybe). So, the parameters to the DXL function that calls a VBA method are oleMethod(ClassOnWhichToCallMethod, cMethodToCall, ArgumentsContainer, ObjectReturn).

Now, let's say instead you want to set a property on a VBA class. You would use oleSet(ObjectToSetPropertyOn, cPropertyName, propertyValue). If you want to retrieve the value of a property, you use oleGet(objFromWhichToGetProperty, cPropertyName, propertyValueReturn).

Always remember to clear(objArgBlock) before adding arguments to it. Always make sure your method is defined in Ole.inc as a cont string. Make sure the parameters that are VBA enumertions are defined in Ole.inc as const integers using the correct number from VBA. wdColorWhite = 0, for example.

In MS Word, you can look at all the classes, methods and enumerations that are available to you. You can also record macros. Basically, if you can do it in MS Word, you can automate it through DXL using it as a wrapper around the VBA code. Hope this helps.

 

woodpryan

Re: Automation word compare
EHcnck - Fri Mar 20 10:50:40 EDT 2015

woodpryan - Fri Mar 20 01:09:25 EDT 2015

This should steer you at least near the right direction:

#include ""utils/ThaadOleDotInc.inc"//Modification of ole.inc, delivered with DOORS by IBM
#include "utils/fileops.dxl"//Provided by DOORS. Gives us some file functions
#include "utils/doctools/rtfattr.inc"//Provided by DOORS. Rich Text Format stuff
#include "utils/doctools/itfutil.inc"

OleAutoObj objDocs         = null;
OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoObj objTables       = null;
OleAutoObj objInlineShapes = null;
OleAutoObj objSel          = null;
OleAutoArgs objArgBlock    = null;

Skip wordStyleNames      = null; // skip list acts as a zero-based array
Skip styleOKNames        = null; // list of styles from "paragraph style" attr that we have verified
Skip wordStyleFonts      = null;  // skip list of fonts corresponding to style names
Skip wordStyleHasBullets = null; // skip list storing word styles which have bullets
Skip wordStyleHasIndents = null; // skip list storing word styles which have indents
int  noWordStyles        = 0;//The number of Word Styles
bool styleWarn           = false;


const string fontTableBeginning = (NLS_("\\sectd\\deff0{\\fonttbl"));

// fonts in this part of the font table will be used for anything
// outside the Latin-1 character set
const string fontTableEnd =
(NLS_("{\\f1\\fswiss\\fprq2\\fcharset0 Arial;}
{\\f2\\froman\\fprq2\\fcharset2 Symbol;}
{\\f3\\fswiss\\fprq2\\fcharset128 Arial Unicode MS;}
{\\f4\\fswiss\\fprq2\\fcharset129 Arial Unicode MS;}
{\\f5\\fswiss\\fprq2\\fcharset130 Arial Unicode MS;}
{\\f6\\fswiss\\fprq2\\fcharset134 Arial Unicode MS;}
{\\f7\\fswiss\\fprq2\\fcharset136 Arial Unicode MS;}
{\\f8\\fswiss\\fcharset161 Arial;}
{\\f9\\fswiss\\fcharset162 Arial;}
{\\f10\\fswiss\\fcharset163 Arial;}
{\\f11\\fswiss\\fcharset177 Arial;}
{\\f12\\fswiss\\fcharset178 Arial;}
{\\f13\\fswiss\\fcharset186 Arial;}
{\\f14\\fswiss\\fcharset204 Arial;}
{\\f15\\fswiss\\fprq2\\fcharset222 Arial Unicode MS;}
{\\f16\\fswiss\\fcharset238 Arial;}
}"));

/* Function taken directly from word.dxl provided by DOORS
   Purpose: This function checks that an oleMthod, oleGet, or olePut
            function did not return an error in the form of a string.
            Wrap this function around one of those three functions, and
            if it returns an error, this function will print it in a message box.
*/
void vbaCheckRes(string res)
{
    if(res != (NLS_("")))
    {
        ack (dxlStrformat(LS_("String_OLE_method_failed_s",NLSTEMP_("OLE method failed: %s")), res))
    }
}

/* Function taken directly from word.dxl provided by DOORS
*/
void getWordStyleNames(OleAutoObj objDoc)
{
    OleAutoObj objStyles = null;
    OleAutoObj objStyle = null;
    OleAutoObj objFont = null;
    OleAutoObj objParaFormat = null;
    OleAutoObj objListTemplate = null;
    string styleName = null;
    string fontName = null;
    int styleType = 0;
    int leftIndent = 0;

    wordStyleNames = create;
    wordStyleFonts = create;
    wordStyleHasBullets = create;
    wordStyleHasIndents = create;

    int count, stylesToCheck;
    noWordStyles = 0;

    vbaCheckRes(oleGet(objDoc, cPropertyStyles, objStyles));

    if (null objStyles)
    {
        ack LS_("String_Unable_to_get_handle_on_styles_collection",NLSTEMP_("Unable to get handle on styles collection"));
        clearWordState;
    }

    vbaCheckRes(oleGet(objStyles, cPropertyCount, stylesToCheck));

    for (count = 1; count <= stylesToCheck; count++)
    {
        clear objArgBlock;
        put(objArgBlock, count);

        vbaCheckRes(oleMethod(objStyles, cMethodItem, objArgBlock, objStyle));

        if (null objStyle)
        {
            ack LS_("String_Unable_to_get_handle_on_style",NLSTEMP_("Unable to get handle on style"));
            clearWordState();
        }

        // Don't use character styles
        vbaCheckRes(oleGet(objStyle, cPropertyType, styleType));

        if (styleType != wdStyleTypeParagraph)
        {
            closeIfNonNull objStyle;
            continue;
        }

        styleName = (NLS_(""));
        fontName = (NLS_(""));
        leftIndent = 0;

        vbaCheckRes(oleGet(objStyle, cPropertyNameLocal, styleName));

        if (null styleName)
        {
            ack LS_("String_Unable_to_get_style_name",NLSTEMP_("Unable to get style name"));
            clearWordState;
        }
        put(wordStyleNames,noWordStyles,styleName);

        // Now check if the style involves bullets or indenting

        oleGet(objStyle, cPropertyListTemplate, objListTemplate);
        if (!(null objListTemplate))
        { // The style has some kind of bulleting applied
            put(wordStyleHasBullets, styleName, styleName);
            oleCloseAutoObject objListTemplate;
        }
        oleGet(objStyle, cPropertyParagraphFormat, objParaFormat);
        oleGet(objParaFormat, cPropertyLeftIndent, leftIndent);
        if (leftIndent > 0)
        { // The style has some kind of indenting applied
            put(wordStyleHasIndents, styleName, styleName);
        }
        closeIfNonNull objParaFormat;

        // get font name so we can set the correct default font
        vbaCheckRes(oleGet(objStyle, cPropertyFont, objFont));

        if (null objFont)
        {
            ack LS_("String_Unable_to_get_handle_on_font",NLSTEMP_("Unable to get handle on font"));
            clearWordState;
        }

        vbaCheckRes(oleGet(objFont, cPropertyName, fontName));

        if (null fontName)
        {
            ack LS_("String_Unable_to_get_font_name",NLSTEMP_("Unable to get font name"));
            clearWordState();
        }

        closeIfNonNull objFont;
        closeIfNonNull objStyle;

        put(wordStyleFonts,styleName,fontName);

        noWordStyles++;
    }

    closeIfNonNull objStyles;

    // <sigh>, Word gives us styles in almost-but-not-quite alpha order,
    // we really do need to sort them
    string tempWordStyles[noWordStyles];

    for (count = 0; count < noWordStyles; count++)
    {
        find(wordStyleNames, count, tempWordStyles[count]);
    }

    sort tempWordStyles;
    delete wordStyleNames;
    wordStyleNames = create;


    for (count = 0; count < noWordStyles; count++)
    {
        put(wordStyleNames, count, tempWordStyles[count]);
    }
}


/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/* Function taken directly from word.dxl provided by DOORS
*/
bool wordPreChecksVBA()
{
    Object curro = current Object;
    Module m     = current Module;

    // Make Word visible
    makeVisible objWord;

    // Get documents collection
    checkRes(oleGet(objWord, cPropertyDocuments, objDocs));

    if (null objDocs)
    {
        ack LS_("String_Unable_to_get_Documents_collection",NLSTEMP_("Unable to get Documents collection"));
        return false;
    }

    // Make a new document
    checkRes(oleMethod(objDocs,cMethodAdd));

    // Get a handle on the document
    checkRes(oleGet(objWord,cPropertyActiveDocument,objDoc));

    if (null objDoc)
    {
        ack LS_("String_Unable_to_get_active_document",NLSTEMP_("Unable to get active document"));
        halt();
    }

    // Get a handle on the selection object
    checkRes(oleGet(objWord,cPropertySelection,objSel));

    if (null objSel)
    {
        ack LS_("String_Unable_to_get_selection_object",NLSTEMP_("Unable to get selection object"));

        halt();
    }

    // Get a handle on the shapes collection
    checkRes(oleGet(objDoc,cPropertyInlineShapes,objInlineShapes));

    if (null objInlineShapes)
    {
        ack LS_("String_Unable_to_get_inline_shapes_collection",NLSTEMP_("Unable to get inline shapes collection"));
        halt();
    }

    // Get a handle on the tables collection
    checkRes(oleGet(objDoc,cPropertyTables,objTables));

    if (null objTables)
    {
        ack LS_("String_Unable_to_get_tables_collection",NLSTEMP_("Unable to get tables collection"));
        halt();
    }

    return true;
}

/* Function taken directly from word.dxl provided by DOORS
*/
string makeFontTableFromStyle(string styleName)
{
// build a font table with the appropriate default font for the style
    string fontName = (NLS_(""))
    string defaultFontEntry
    string fontTable

    find(wordStyleFonts, styleName, fontName)

    defaultFontEntry = (NLS_("{\\f0\\fnil ")) fontName (NLS_(";}"))
    fontTable = fontTableBeginning defaultFontEntry fontTableEnd

    return fontTable
}

/*  Function:   toggleTrackRevision()
    Purpose:    toggles trackRevision on and off in the document
*/
void toggleTrackRevisions()
{
    bool track = false;

    oleGet(objDoc, cPropertyTrackRevisions, track);
    olePut(objDoc, cPropertyTrackRevisions, !track);
    isTrackRevisions = !track;
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name.
*/
void createDocumentDiff(string original, string revised)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

    oleGet(objWord, cPropertyDocuments, objDocuments);
    if(null == objDocuments)
    {
        logError("Could not retrieve the Documents collection from MS Word");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, original);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc);
    if(null == objOriginalDoc)
    {
        closeIfNonNull(objDocuments);
        logError("Could not retrieve the Original Document from the Documents Collection.");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, revised);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
    if(null == objRevisedDoc)
    {
        closeIfNonNull(objDocuments);
        closeIfNonNull(objOriginalDoc);
        logError("Could not retrieve the Revised Document from the Documents Collection.");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, objOriginalDoc);//originalDocument
    put(objArgBlock, objRevisedDoc);//revisedDocument
    put(objArgBlock, wdCompareDestinationNew);//Destination
    put(objArgBlock, wdGranularityCharLevel);//granularity
    put(objArgBlock, false);//compareFormatting
    put(objArgBlock, true);//compareCaseChanges
    put(objArgBlock, true);//compareWhitespace
    put(objArgBlock, true);//compareTables
    put(objArgBlock, false);//compareHeaders
    put(objArgBlock, false);//compareFootnotes
    put(objArgBlock, false);//compareTextboxes
    put(objArgBlock, false);//compareFields
    put(objArgBlock, false);//compareComments
    put(objArgBlock, false);//compareMoves


    oleMethod(objWord, cMethodCompareDocuments, objArgBlock);

    oleGet(objWord, cPropertyDocuments, objDocuments);

    clear(objArgBlock);
    put(objArgBlock, 1);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);
    oleMethod(objDoc, cMethodActivate);

    closeIfNonNull(objDocuments);
    closeIfNonNull(objOriginalDoc);
    closeIfNonNull(objRevisedDoc);
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/*  Function:   activateDocument(string)
    Purpose:    Takes in a document name, finds the document
                in the documents collection by name, and makes that document active.
    Parameters: The name of the document to make active.
*/
void activateDocument(string docName)
{
    OleAutoObj objDocuments = null;
    string     documentName = null;

    oleGet(objWord, cPropertyDocuments, objDocuments);
    if(null == objDocuments)
    {
        logError("Could not retrieve the Documents collection from MS Word");
        return;
    }

    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);

    if(null == objDoc)
    {
        logError("Could not find a document in the collection named " docName);
        return;
    }

    oleMethod(objDoc, cMethodActivate);
    oleGet(objWord, cPropertySelection, objSel);
    activeDocumentName = docName;
}

/*  Function:   saveDocument(string)
    Purpose:    saves the active document using the given
                document name.
    Parameters: the name of the document
*/
void saveDocument(string docName)
{
    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
}

Think of an OleAutoObj like this. Each class in VBA (Word, Document, Selection, etc.) is an OleAutoObj. Each function available to a class is a cMethod. Each cMethod is define in Ole.inc as cMethodName = "Name". When you want to call a method in VBA on a class, you must pass it arguments. Those arguments go, in order, into the objArbBlock, which is a container for the arguments. When you call the method, you must store the return value in something else (another OleAutoObj, maybe). So, the parameters to the DXL function that calls a VBA method are oleMethod(ClassOnWhichToCallMethod, cMethodToCall, ArgumentsContainer, ObjectReturn).

Now, let's say instead you want to set a property on a VBA class. You would use oleSet(ObjectToSetPropertyOn, cPropertyName, propertyValue). If you want to retrieve the value of a property, you use oleGet(objFromWhichToGetProperty, cPropertyName, propertyValueReturn).

Always remember to clear(objArgBlock) before adding arguments to it. Always make sure your method is defined in Ole.inc as a cont string. Make sure the parameters that are VBA enumertions are defined in Ole.inc as const integers using the correct number from VBA. wdColorWhite = 0, for example.

In MS Word, you can look at all the classes, methods and enumerations that are available to you. You can also record macros. Basically, if you can do it in MS Word, you can automate it through DXL using it as a wrapper around the VBA code. Hope this helps.

 

woodpryan

Not sure what I 'm doing work but getting an error message "Could not retrieve the Orginal Document from the Documents Collection."

 

#include <utils/ole.inc>

//------------------------ VBA Methods ---------------------------
const string cMethodSaveAs           =                          (NLS_("SaveAs"))
const string cMethodCompareDocuments =                          (NLS_("MergeDocuments"))
const string wdCompareDestinationNew =                          (NLS_("wdCompareDestinationNew"))
const string wdGranularityWordLevel  =                          (NLS_("wdGranularityWordLevel"))

OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoArgs objArgBlock    = null;

/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name, the compare document name.
*/
void createDocumentDiff(string original, string revised, string docName)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

        oleGet(objWord, cPropertyDocuments, objDocuments)
        if(null == objDocuments) {
                errorBox("Could not retrieve the Documents collection from MS Word")
                return
        }

        clear(objArgBlock)
        put(objArgBlock, original)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc)
        if(null == objOriginalDoc) {
                closeIfNonNull(objDocuments)
                errorBox("Could not retrieve the Original Document from the Documents Collection.")
                return
        }
        
        clear(objArgBlock)
        put(objArgBlock, revised)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
        if(null == objRevisedDoc) {
                closeIfNonNull(objDocuments)
                closeIfNonNull(objOriginalDoc)
                errorBox("Could not retrieve the Revised Document from the Documents Collection.")
                return
        }

        clear(objArgBlock)
        put(objArgBlock, objOriginalDoc /*originalDocument*/)
        put(objArgBlock, objRevisedDoc /*revisedDocument */)
        put(objArgBlock, wdCompareDestinationNew /*Destination*/)
        put(objArgBlock, wdGranularityWordLevel /*granularity*/ )
        put(objArgBlock, true /*compareFormatting*/ )             
        put(objArgBlock, true /*compareCaseChanges*/)             
        put(objArgBlock, true /*compareWhitespace*/)         
        put(objArgBlock, true /*compareTables*/)     
        put(objArgBlock, true /*compareHeaders*/)
        put(objArgBlock, true /*compareFootnotes*/)
        put(objArgBlock, true /*compareTextboxes*/)
        put(objArgBlock, true /*compareFields*/)
        put(objArgBlock, true /*compareComments*/)
        put(objArgBlock, true /*compareMoves*/)
        put(objArgBlock, "rpe" /*OrginalAuthor*/)
        put(objArgBlock, "rpe" /*RevisedAuthor*/)

        oleMethod(objWord, cMethodCompareDocuments, objArgBlock)
        oleGet(objWord, cPropertyDocuments, objDocuments)

        clear(objArgBlock)
        put(objArgBlock, 1)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc)
        oleMethod(objDoc, cMethodActivated)

    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
        
        closeIfNonNull(objDocuments)
        closeIfNonNull(objOriginalDoc)
        closeIfNonNull(objRevisedDoc)
}

createWordApplication()
createDocumentDiff("P:\\Desktop\\864506-1325-221E002(Current).doc", "P:\\Desktop\\864506-1325-221E002(Current) - Copy.doc", "P:\\Desktop\\Compare.docm")

 

Re: Automation word compare
Mathias Mamsch - Fri Mar 20 10:58:09 EDT 2015

EHcnck - Fri Mar 20 10:50:40 EDT 2015

Not sure what I 'm doing work but getting an error message "Could not retrieve the Orginal Document from the Documents Collection."

 

#include <utils/ole.inc>

//------------------------ VBA Methods ---------------------------
const string cMethodSaveAs           =                          (NLS_("SaveAs"))
const string cMethodCompareDocuments =                          (NLS_("MergeDocuments"))
const string wdCompareDestinationNew =                          (NLS_("wdCompareDestinationNew"))
const string wdGranularityWordLevel  =                          (NLS_("wdGranularityWordLevel"))

OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoArgs objArgBlock    = null;

/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name, the compare document name.
*/
void createDocumentDiff(string original, string revised, string docName)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

        oleGet(objWord, cPropertyDocuments, objDocuments)
        if(null == objDocuments) {
                errorBox("Could not retrieve the Documents collection from MS Word")
                return
        }

        clear(objArgBlock)
        put(objArgBlock, original)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc)
        if(null == objOriginalDoc) {
                closeIfNonNull(objDocuments)
                errorBox("Could not retrieve the Original Document from the Documents Collection.")
                return
        }
        
        clear(objArgBlock)
        put(objArgBlock, revised)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
        if(null == objRevisedDoc) {
                closeIfNonNull(objDocuments)
                closeIfNonNull(objOriginalDoc)
                errorBox("Could not retrieve the Revised Document from the Documents Collection.")
                return
        }

        clear(objArgBlock)
        put(objArgBlock, objOriginalDoc /*originalDocument*/)
        put(objArgBlock, objRevisedDoc /*revisedDocument */)
        put(objArgBlock, wdCompareDestinationNew /*Destination*/)
        put(objArgBlock, wdGranularityWordLevel /*granularity*/ )
        put(objArgBlock, true /*compareFormatting*/ )             
        put(objArgBlock, true /*compareCaseChanges*/)             
        put(objArgBlock, true /*compareWhitespace*/)         
        put(objArgBlock, true /*compareTables*/)     
        put(objArgBlock, true /*compareHeaders*/)
        put(objArgBlock, true /*compareFootnotes*/)
        put(objArgBlock, true /*compareTextboxes*/)
        put(objArgBlock, true /*compareFields*/)
        put(objArgBlock, true /*compareComments*/)
        put(objArgBlock, true /*compareMoves*/)
        put(objArgBlock, "rpe" /*OrginalAuthor*/)
        put(objArgBlock, "rpe" /*RevisedAuthor*/)

        oleMethod(objWord, cMethodCompareDocuments, objArgBlock)
        oleGet(objWord, cPropertyDocuments, objDocuments)

        clear(objArgBlock)
        put(objArgBlock, 1)
        oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc)
        oleMethod(objDoc, cMethodActivated)

    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
        
        closeIfNonNull(objDocuments)
        closeIfNonNull(objOriginalDoc)
        closeIfNonNull(objRevisedDoc)
}

createWordApplication()
createDocumentDiff("P:\\Desktop\\864506-1325-221E002(Current).doc", "P:\\Desktop\\864506-1325-221E002(Current) - Copy.doc", "P:\\Desktop\\Compare.docm")

 

Since the method assumes that the documents are already opened (it accesses the documents collection), you first need to open the documents and then access the Documents collection by the filename only (without path). Or you need to change the diff method, so that it will open the documents from a file.

Also I would say that removing special characters like "(" and ")" from filenames is always a good idea. It should work, but you never know ;-)

Regards, Mathias

Re: Automation word compare
EHcnck - Fri Mar 20 11:29:33 EDT 2015

Mathias Mamsch - Fri Mar 20 10:58:09 EDT 2015

Since the method assumes that the documents are already opened (it accesses the documents collection), you first need to open the documents and then access the Documents collection by the filename only (without path). Or you need to change the diff method, so that it will open the documents from a file.

Also I would say that removing special characters like "(" and ")" from filenames is always a good idea. It should work, but you never know ;-)

Regards, Mathias

When I open the docs the dxl runs but the compare file if corrupted any ideas why?

Re: Automation word compare
woodpryan - Fri Mar 20 11:32:33 EDT 2015

/*  Function:   openDocument(string)
    Purpose:    opens a document having the given name and updates the objDoc
                global variable. Logs an error if the document could not be opened.
    Parameters: the full path to the document
*/
void openDocument(string docName)
{
    
    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDocs, cMethodOpen, objArgBlock, objDoc);
    
    if(null == objDoc)
        print("Could not open the document named " docName);
}

Try adding this.

Re: Automation word compare
Mathias Mamsch - Fri Mar 20 12:08:48 EDT 2015

EHcnck - Fri Mar 20 11:29:33 EDT 2015

When I open the docs the dxl runs but the compare file if corrupted any ideas why?

Probably because you saved a .docx file as .doc or vice versa. Regards, Mathias

Re: Automation word compare
EHcnck - Fri Mar 20 12:23:15 EDT 2015

Mathias Mamsch - Fri Mar 20 12:08:48 EDT 2015

Probably because you saved a .docx file as .doc or vice versa. Regards, Mathias

I fix the issue with the save but now the actual compare functionality doesn't seem to be working. Any thoughts? I greatly appreciate the help.

 

 

#include <utils/ole.inc>

//------------------------ VBA Methods ---------------------------
const string cMethodSaveAs           =       (NLS_("SaveAs"))
const string cMethodCompareDocuments =    (NLS_("MergeDocuments"))
const string wdCompareDestinationNew =    (NLS_("wdCompareDestinationNew"))
const string wdGranularityWordLevel  =    (NLS_("wdGranularityWordLevel"))
const string cMethodOpen             =    (NLS_("Open"))

OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoObj objDocs         = null;
OleAutoArgs objArgBlock    = null;


/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/*  Function:   openDocument(string)
    Purpose:    opens a document having the given name and updates the objDoc
                global variable. Logs an error if the document could not be opened.
    Parameters: the full path to the document
*/
void openDocument(string docName)
{
    olePut(objWord, cPropertyVisible, true)
    oleGet(objWord, cPropertyDocuments, objDoc)
  
    // open file
 clear(objArgBlock);
    put(objArgBlock, docName)
    oleMethod(objDoc, cMethodOpen, objArgBlock)
    
    if(null == objDoc)
        errorBox("Could not open the document named " docName);
}

/*  Function:   saveDocument(string)
    Purpose:    saves the active document using the given
                document name.
    Parameters: the name of the document
*/
void saveDocument(string docName)
{
    clear(objArgBlock);
    put(objArgBlock, docName);
 put(objArgBlock, 13); //wdFormatXMLDocumentMacroEnabled
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name.
*/
void createDocumentDiff(string original, string revised)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

 oleGet(objWord, cPropertyDocuments, objDocuments)
 if(null == objDocuments) {
  errorBox("Could not retrieve the Documents collection from MS Word")
  return
 }

 clear(objArgBlock)
 put(objArgBlock, original)
 oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc)
 if(null == objOriginalDoc) {
  closeIfNonNull(objDocuments)
  errorBox("Could not retrieve the Original Document from the Documents Collection.")
  return
 }
 
 clear(objArgBlock)
 put(objArgBlock, revised)
 oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
 if(null == objRevisedDoc) {
  closeIfNonNull(objDocuments)
  closeIfNonNull(objOriginalDoc)
  errorBox("Could not retrieve the Revised Document from the Documents Collection.")
  return
 }

    clear(objArgBlock);
    put(objArgBlock, objOriginalDoc);//originalDocument
    put(objArgBlock, objRevisedDoc);//revisedDocument
    put(objArgBlock, wdCompareDestinationNew);//Destination
    put(objArgBlock, wdGranularityWordLevel);//granularity
    put(objArgBlock, true);//compareFormatting
    put(objArgBlock, true);//compareCaseChanges
    put(objArgBlock, true);//compareWhitespace
    put(objArgBlock, true);//compareTables
    put(objArgBlock, true);//compareHeaders
    put(objArgBlock, true);//compareFootnotes
    put(objArgBlock, true);//compareTextboxes
    put(objArgBlock, true);//compareFields
    put(objArgBlock, true);//compareComments
    put(objArgBlock, true);//compareMoves

    oleMethod(objWord, cMethodCompareDocuments, objArgBlock);

    oleGet(objWord, cPropertyDocuments, objDocuments);

    clear(objArgBlock);
    put(objArgBlock, 1);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);
    oleMethod(objDoc, cMethodActivated);
 
 closeIfNonNull(objDocuments)
 closeIfNonNull(objOriginalDoc)
 closeIfNonNull(objRevisedDoc)
}

createWordApplication()
openDocument("P:\\Desktop\\test.doc")  
openDocument("P:\\Desktop\\test1.doc")
createDocumentDiff("test.doc", "test1.doc")
saveDocument("P:\\Desktop\\compare.docm")

 

Re: Automation word compare
woodpryan - Fri Mar 20 12:49:37 EDT 2015

EHcnck - Fri Mar 20 12:23:15 EDT 2015

I fix the issue with the save but now the actual compare functionality doesn't seem to be working. Any thoughts? I greatly appreciate the help.

 

 

#include <utils/ole.inc>

//------------------------ VBA Methods ---------------------------
const string cMethodSaveAs           =       (NLS_("SaveAs"))
const string cMethodCompareDocuments =    (NLS_("MergeDocuments"))
const string wdCompareDestinationNew =    (NLS_("wdCompareDestinationNew"))
const string wdGranularityWordLevel  =    (NLS_("wdGranularityWordLevel"))
const string cMethodOpen             =    (NLS_("Open"))

OleAutoObj objWord         = null;
OleAutoObj objDoc          = null;
OleAutoObj objDocs         = null;
OleAutoArgs objArgBlock    = null;


/*
    Function: creatWordApplication()
    Purpose: creates the word application.
    Taken directly from word.dxl provided by DOORS
*/
void createWordApplication()
{
    objArgBlock = create();

    // attaches to an existing word application
    objWord = oleGetAutoObject(cObjWordApplication);

    if (null objWord)
    {
        // creates a new word application
        objWord = oleCreateAutoObject(cObjWordApplication);
    }
}

/*  Function:   openDocument(string)
    Purpose:    opens a document having the given name and updates the objDoc
                global variable. Logs an error if the document could not be opened.
    Parameters: the full path to the document
*/
void openDocument(string docName)
{
    olePut(objWord, cPropertyVisible, true)
    oleGet(objWord, cPropertyDocuments, objDoc)
  
    // open file
 clear(objArgBlock);
    put(objArgBlock, docName)
    oleMethod(objDoc, cMethodOpen, objArgBlock)
    
    if(null == objDoc)
        errorBox("Could not open the document named " docName);
}

/*  Function:   saveDocument(string)
    Purpose:    saves the active document using the given
                document name.
    Parameters: the name of the document
*/
void saveDocument(string docName)
{
    clear(objArgBlock);
    put(objArgBlock, docName);
 put(objArgBlock, 13); //wdFormatXMLDocumentMacroEnabled
    oleMethod(objDoc, cMethodSaveAs, objArgBlock);
}

/*  Function:   createDocumentDiff(string, string)
    Purpose:    Creates a diff file using the two existing documents
                having the given names. Creates a new document and makes
                it the active document.
    Parameters: The original document name, the revised document name.
*/
void createDocumentDiff(string original, string revised)
{
    OleAutoObj objDocuments   = null;
    OleAutoObj objOriginalDoc = null;
    OleAutoObj objRevisedDoc  = null;

 oleGet(objWord, cPropertyDocuments, objDocuments)
 if(null == objDocuments) {
  errorBox("Could not retrieve the Documents collection from MS Word")
  return
 }

 clear(objArgBlock)
 put(objArgBlock, original)
 oleMethod(objDocuments, cMethodItem, objArgBlock, objOriginalDoc)
 if(null == objOriginalDoc) {
  closeIfNonNull(objDocuments)
  errorBox("Could not retrieve the Original Document from the Documents Collection.")
  return
 }
 
 clear(objArgBlock)
 put(objArgBlock, revised)
 oleMethod(objDocuments, cMethodItem, objArgBlock, objRevisedDoc);
 if(null == objRevisedDoc) {
  closeIfNonNull(objDocuments)
  closeIfNonNull(objOriginalDoc)
  errorBox("Could not retrieve the Revised Document from the Documents Collection.")
  return
 }

    clear(objArgBlock);
    put(objArgBlock, objOriginalDoc);//originalDocument
    put(objArgBlock, objRevisedDoc);//revisedDocument
    put(objArgBlock, wdCompareDestinationNew);//Destination
    put(objArgBlock, wdGranularityWordLevel);//granularity
    put(objArgBlock, true);//compareFormatting
    put(objArgBlock, true);//compareCaseChanges
    put(objArgBlock, true);//compareWhitespace
    put(objArgBlock, true);//compareTables
    put(objArgBlock, true);//compareHeaders
    put(objArgBlock, true);//compareFootnotes
    put(objArgBlock, true);//compareTextboxes
    put(objArgBlock, true);//compareFields
    put(objArgBlock, true);//compareComments
    put(objArgBlock, true);//compareMoves

    oleMethod(objWord, cMethodCompareDocuments, objArgBlock);

    oleGet(objWord, cPropertyDocuments, objDocuments);

    clear(objArgBlock);
    put(objArgBlock, 1);
    oleMethod(objDocuments, cMethodItem, objArgBlock, objDoc);
    oleMethod(objDoc, cMethodActivated);
 
 closeIfNonNull(objDocuments)
 closeIfNonNull(objOriginalDoc)
 closeIfNonNull(objRevisedDoc)
}

createWordApplication()
openDocument("P:\\Desktop\\test.doc")  
openDocument("P:\\Desktop\\test1.doc")
createDocumentDiff("test.doc", "test1.doc")
saveDocument("P:\\Desktop\\compare.docm")

 

Replace your Ole.inc file with the one I've attached to this response. It includes the correct DXL style definitions of every MS Word VBA method, property, and enumeration. Then, change the last line of code. You've saved the compare doc with a '.docm' extension. Use '.doc'. Remember to delete those cMethod, property, and enumeration definitions at the top of your code. Otherwise, some of those variables will be stepping on what's defined in Ole.inc.


Attachments

Ole.inc

Re: Automation word compare
EHcnck - Fri Mar 20 13:16:38 EDT 2015

woodpryan - Fri Mar 20 11:32:33 EDT 2015

/*  Function:   openDocument(string)
    Purpose:    opens a document having the given name and updates the objDoc
                global variable. Logs an error if the document could not be opened.
    Parameters: the full path to the document
*/
void openDocument(string docName)
{
    
    clear(objArgBlock);
    put(objArgBlock, docName);
    oleMethod(objDocs, cMethodOpen, objArgBlock, objDoc);
    
    if(null == objDoc)
        print("Could not open the document named " docName);
}

Try adding this.

Thank you for all the help. I got to working but when I add the following lines the compare doesn't work:

 

    put(objArgBlock, true);//compareTextboxes
    put(objArgBlock, true);//compareFields
    put(objArgBlock, true);//compareComments
    put(objArgBlock, true);//compareMoves
    //put(objArgBlock, "rpe"); //OriginalAuthor <- this doesn't work
    //put(objArgBlock, "rpe"); //RevisedAuthor

What would be the best way to do garbage clean closing open word docs and free any memory.

Re: Automation word compare
woodpryan - Fri Mar 20 13:30:49 EDT 2015

First, garbage cleanup. Create a function to call at the end of execution. Call it cleanUp or something. In that function, call closeIfNonNull(OleAutoObj) on any globally declared OleAutoObj variables. If you create any of those in a smaller scope, make sure you delete them there. Also, make sure you delete any skip lists that have been created. If you want to close the original and revised documents, write a DXL function, call it void close(string docName). In that function try figuring out how to close a document having the given name. Use the examples you have for how to retrieve a document from the collection of documents and how to call the VBA close function.

As for the two lines of code you have at the bottom, I'm not sure what "rpe" is, but you want to retrieve cPropertyAuthor from the document. Write a function called string getDocumentAuthor(string docName), which returns the author name. You'll need to get the given document from the collection of documents and retrieve the cPropertyAuthor, then return it to the caller as a string. Keep in mind what I said about calling VBA methods and getting/setting VBA properties at the bottom of my first post.

Re: Automation word compare
EHcnck - Fri Mar 20 14:18:11 EDT 2015

woodpryan - Fri Mar 20 13:30:49 EDT 2015

First, garbage cleanup. Create a function to call at the end of execution. Call it cleanUp or something. In that function, call closeIfNonNull(OleAutoObj) on any globally declared OleAutoObj variables. If you create any of those in a smaller scope, make sure you delete them there. Also, make sure you delete any skip lists that have been created. If you want to close the original and revised documents, write a DXL function, call it void close(string docName). In that function try figuring out how to close a document having the given name. Use the examples you have for how to retrieve a document from the collection of documents and how to call the VBA close function.

As for the two lines of code you have at the bottom, I'm not sure what "rpe" is, but you want to retrieve cPropertyAuthor from the document. Write a function called string getDocumentAuthor(string docName), which returns the author name. You'll need to get the given document from the collection of documents and retrieve the cPropertyAuthor, then return it to the caller as a string. Keep in mind what I said about calling VBA methods and getting/setting VBA properties at the bottom of my first post.

How would one iterate on a document collection to close all the word docs opened? Not sure how one would do this via DXL.

Re: Automation word compare
woodpryan - Fri Mar 20 14:57:16 EDT 2015

EHcnck - Fri Mar 20 14:18:11 EDT 2015

How would one iterate on a document collection to close all the word docs opened? Not sure how one would do this via DXL.

Well, the first question you have to ask yourself is, 'How would I do this in VBA?'

With MS Word automation, that should always be your first question. To answer it, the Documents collection has a number of documents in it. That number may be accessed by retrieving the Count property on the documents collection. The documents in that collectioin may be accessed in two ways. You can retrieve them using the Item function, passing the function the name of the document or you can retrieve them using the Item function, passing it an index, which begins at 1, not 0. Once you've got a handle on the document, you can close it using the Close function.

Now, you want to do this through DXL. Write yourself another close(int) function that takes an index to the documents collection instead of a name. Write another function that returns the number of documents currently in the documents collection. Call close(1) until the return of getNumOpenDocuments is 0.

Re: Automation word compare
EHcnck - Fri Mar 20 18:48:10 EDT 2015

woodpryan - Fri Mar 20 14:57:16 EDT 2015

Well, the first question you have to ask yourself is, 'How would I do this in VBA?'

With MS Word automation, that should always be your first question. To answer it, the Documents collection has a number of documents in it. That number may be accessed by retrieving the Count property on the documents collection. The documents in that collectioin may be accessed in two ways. You can retrieve them using the Item function, passing the function the name of the document or you can retrieve them using the Item function, passing it an index, which begins at 1, not 0. Once you've got a handle on the document, you can close it using the Close function.

Now, you want to do this through DXL. Write yourself another close(int) function that takes an index to the documents collection instead of a name. Write another function that returns the number of documents currently in the documents collection. Call close(1) until the return of getNumOpenDocuments is 0.

Trying to set all values on the tracking tab, I can figure out why "cPropertyTrackMoves, cPropertyTrackFormatting" aren't working.

 

/*  Function:   toggleTrackRevision()
    Purpose:    sets appropriate tracking settings
*/
void toggleTrackRevisions()
{
    /************************WdColorIndex**********************/
        //Override ole.inc colour not mapped correct for Word 2007
        const int wdGreen =  12;

    olePut(objDoc, cPropertyTrackRevisions, true)

        OleAutoObj objOptions = null
        oleGet(objWordApplication, cPropertyOptions, objOptions)
        
    olePut(objOptions, cPropertyInsertedTextMark, wdInsertedTextMarkUnderline)
    olePut(objOptions, cPropertyInsertedTextColor, wdByAuthor)
    olePut(objOptions, cPropertyDeletedTextMark, wdDeletedTextMarkStrikeThrough)
    olePut(objOptions, cPropertyDeletedTextColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisedPropertiesMark, wdRevisedPropertiesMarkNone)
    olePut(objOptions, cPropertyRevisedPropertiesColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisedLinesMark, wdRevisedLinesMarkRightBorder)
    olePut(objOptions, cPropertyRevisedLinesColor, wdAuto)
    olePut(objOptions, cPropertyCommentsColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisionsBalloonPrintOrientation, wdBalloonPrintOrientationPreserve)
        
    olePut(objOptions, cPropertyMoveFromTextMark, wdMoveFromTextMarkDoubleStrikeThrough)
    olePut(objOptions, cPropertyMoveFromTextColor, wdGreen)
    olePut(objOptions, cPropertyMoveToTextMark, wdMoveToTextMarkDoubleUnderline)
    olePut(objOptions, cPropertyMoveToTextColor, wdGreen)
    olePut(objOptions, cPropertyInsertedCellColor, wdCellColorLightBlue)
    olePut(objOptions, cPropertyMergedCellColor, wdCellColorLightYellow)
    olePut(objOptions, cPropertyDeletedCellColor, wdCellColorPink)
    olePut(objOptions, cPropertySplitCellColor, wdCellColorLightOrange)
                
        OleAutoObj objWindow = null
        OleAutoObj objView = null
        oleGet(objWordApplication, cPropertyActiveWindow, objWindow)
        oleGet(objWindow, cPropertyView, objView)
        olePut(objView, "RevisionsMode", wdMixedRevisions)

        OleAutoObj objADoc = null
        oleGet(objWordApplication, cPropertyActiveDocument, objADoc)
        olePut(objADoc, cPropertyTrackMoves, true)
        olePut(objADoc, cPropertyTrackFormatting, false)

        closeIfNonNull(objOptions)
        closeIfNonNull(objWindow)
        closeIfNonNull(objView)
        closeIfNonNull(objADoc)
}

 


Attachments

WordDiff.dxl

Re: Automation word compare
woodpryan - Mon Mar 23 11:06:25 EDT 2015

EHcnck - Fri Mar 20 18:48:10 EDT 2015

Trying to set all values on the tracking tab, I can figure out why "cPropertyTrackMoves, cPropertyTrackFormatting" aren't working.

 

/*  Function:   toggleTrackRevision()
    Purpose:    sets appropriate tracking settings
*/
void toggleTrackRevisions()
{
    /************************WdColorIndex**********************/
        //Override ole.inc colour not mapped correct for Word 2007
        const int wdGreen =  12;

    olePut(objDoc, cPropertyTrackRevisions, true)

        OleAutoObj objOptions = null
        oleGet(objWordApplication, cPropertyOptions, objOptions)
        
    olePut(objOptions, cPropertyInsertedTextMark, wdInsertedTextMarkUnderline)
    olePut(objOptions, cPropertyInsertedTextColor, wdByAuthor)
    olePut(objOptions, cPropertyDeletedTextMark, wdDeletedTextMarkStrikeThrough)
    olePut(objOptions, cPropertyDeletedTextColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisedPropertiesMark, wdRevisedPropertiesMarkNone)
    olePut(objOptions, cPropertyRevisedPropertiesColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisedLinesMark, wdRevisedLinesMarkRightBorder)
    olePut(objOptions, cPropertyRevisedLinesColor, wdAuto)
    olePut(objOptions, cPropertyCommentsColor, wdByAuthor)
    olePut(objOptions, cPropertyRevisionsBalloonPrintOrientation, wdBalloonPrintOrientationPreserve)
        
    olePut(objOptions, cPropertyMoveFromTextMark, wdMoveFromTextMarkDoubleStrikeThrough)
    olePut(objOptions, cPropertyMoveFromTextColor, wdGreen)
    olePut(objOptions, cPropertyMoveToTextMark, wdMoveToTextMarkDoubleUnderline)
    olePut(objOptions, cPropertyMoveToTextColor, wdGreen)
    olePut(objOptions, cPropertyInsertedCellColor, wdCellColorLightBlue)
    olePut(objOptions, cPropertyMergedCellColor, wdCellColorLightYellow)
    olePut(objOptions, cPropertyDeletedCellColor, wdCellColorPink)
    olePut(objOptions, cPropertySplitCellColor, wdCellColorLightOrange)
                
        OleAutoObj objWindow = null
        OleAutoObj objView = null
        oleGet(objWordApplication, cPropertyActiveWindow, objWindow)
        oleGet(objWindow, cPropertyView, objView)
        olePut(objView, "RevisionsMode", wdMixedRevisions)

        OleAutoObj objADoc = null
        oleGet(objWordApplication, cPropertyActiveDocument, objADoc)
        olePut(objADoc, cPropertyTrackMoves, true)
        olePut(objADoc, cPropertyTrackFormatting, false)

        closeIfNonNull(objOptions)
        closeIfNonNull(objWindow)
        closeIfNonNull(objView)
        closeIfNonNull(objADoc)
}

 

Assuming you modified the name of the global variable, objWord, which represents the word application, there is nothing wrong with the above code except that you are hard coding a string in that olePut(objView, "RevisionsMod", wdMixedRevisions) call. I'm not sure where that property even came from. I don't see that in my VBA properties list under Word. Maybe it's one they left out? Anyway, you should add your "RevisionsMode" as a const string cProperty guy so it doesn't look hard coded like that. The reason I don't do that is, the more often you type that string in there, the more likely you are to fat-finger it.

But, if you did not purposely modify the name of the global variable objWord, you may want to fix that part of your code. Also, keep in mind, you have little reason to re-get the ActiveDocument property. Remember that objDoc is always the currently active document (assuming you update it whenever you change the active document).

 

woodpryan